﻿using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Text.RegularExpressions;

namespace CoreWebApiNuGet.Convention
{
    public class ControllerRouteConvention : IControllerModelConvention
    {
        /// <summary>
        /// 重写ApplyF方法实现控制器名称转小写
        /// </summary>
        /// <param name="controller"></param>
        public void Apply(ControllerModel controller)
        {
            // 将控制器名称转换为小写
            controller.ControllerName = AsLowerCase(controller.ControllerName);
        }

        /// <summary>
        /// 转小写字母
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AsLowerCase(string value)
        {
            return Regex.Replace(value, "([A-Z])", "-$1").ToLower().TrimStart('-');
        }
    }
}
